package com.smart.campus.pos.service.impl;

import com.smart.campus.pos.domain.Cost;
import com.smart.campus.pos.exception.PosException;
import com.smart.campus.pos.mapper.CostMapper;
import com.smart.campus.pos.service.CostService;
import com.smart.campus.pos.vo.PosCost;
import com.smart.campus.pos.vo.PosInfo;
import com.smart.campus.pos.vo.PosUser;
import com.smart.campus.school.domain.Pos;
import com.smart.campus.school.domain.Student;
import com.smart.campus.school.service.IPosService;
import com.smart.campus.school.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;

@Service
public class CostServiceImpl implements CostService {

    @Autowired
    private IPosService posService;
    @Autowired
    private IStudentService studentService;
    @Autowired
    private CostMapper costMapper;


    @Override
    public List<PosUser> findCompanyMemberList(Integer pageIndex, Integer pageSize) {
        Student query = new Student();
        query.setInPos(0);
        List<Student> studentList = studentService.selectStudentList(query);
        if(studentList ==null || studentList.size()==0){
            throw new PosException(10028,"当前页不存在");
        }
        List<PosUser> posUserList = new ArrayList<PosUser>();
        for(int i = 0 ; i < studentList.size(); i++){
            Student student = studentList.get(i);

            PosUser user = new PosUser();
            user.setCardState(32);//32:正常卡 33:解挂未完全卡  64:注销卡  96:挂失未完全卡 160:回收卡
            user.setUniqueNumber(student.getId());
            user.setUid(student.getStudentNo());
            user.setCardNumber(student.getStudentNo());//学号作为卡号
            user.setFullName(student.getStudentName());
            user.setBalance(student.getBalance());
            user.setImgData(student.getImg());
            user.setLimitTimes1(255);
            user.setLimitTimes2(255);
            user.setLimitTimes3( 255);
            user.setLimitTimes4(255);
            user.setConsumptionQuota(10000.0000);

            posUserList.add(user);
        }
        return posUserList;
    }

    @Override
    public PosCost modifyBalance(String machineNumber, String cardNumber, int deductionType, String money, int consumptionType, String orderNumber) {
        Student student = this.studentService.selectStudentByStudentNo(cardNumber);
        if(student.getBalance().compareTo(new BigDecimal(money)) == -1){
            //余额不足
            throw new PosException(10019,"余额不足,无法消费");
        }
        Cost cost = new Cost();
        cost.setAmount(new BigDecimal(money));
        cost.setCardNo(cardNumber);
        cost.setCreateTime(new Date());
        cost.setPosNo(machineNumber);
        cost.setUserId(student.getId());
        cost.setOrderNo(orderNumber);
        costMapper.insertCost(cost);

        student.setBalance(student.getBalance().subtract(new BigDecimal(money)));
        studentService.updateStudent(student);

        PosCost posCost = new PosCost();
        posCost.setBalance(student.getBalance());
        posCost.setFullName(student.getStudentName());
        posCost.setConsumptionMone(new BigDecimal(money));
        return posCost;
    }

    @Override
    public PosInfo reportDeviceStatus(String machineNumber) {
        Pos pos = posService.selectPosByPosNo(machineNumber);
        if(pos==null){
            throw new PosException(10029,"设备信息不存在");
        }
        PosInfo posInfo = new PosInfo();
        if(pos.getPosStatus()==1){
            posInfo.setIsOpen(1);
        }
        if(pos.getPosStatus()==2){
            posInfo.setIsOpen(0);
        }
        posInfo.setUpdateConfig("0");
        posInfo.setUpdateUserInfo("1");
        return posInfo;
    }

   /* @Override
    public PosUser getFullRyxxByCardNo(String cardNumber) {
        Tcommaccount tcommaccount = accountFeignClient.getTcommaccountByCardNo(cardNumber);
        if(tcommaccount==null){
            throw new PosException(10008,"无效卡号");
        }
        PosUser user = new PosUser();
        user.setUniqueNumber(tcommaccount.getAccId());
        user.setCardClass("A");

        if("1".equals(tcommaccount.getCardStatus())){
            user.setCardState(32);//32:正常卡 33:解挂未完全卡  64:注销卡  96:挂失未完全卡 160:回收卡
        } else if("0".equals(tcommaccount.getCardStatus())){
            user.setCardState(96);//32:正常卡 33:解挂未完全卡  64:注销卡  96:挂失未完全卡 160:回收卡
        }else if("9".equals(tcommaccount.getCardStatus())){
            user.setCardState(64);//32:正常卡 33:解挂未完全卡  64:注销卡  96:挂失未完全卡 160:回收卡
        }
        user.setUid(tcommaccount.getAccId()+"");
        user.setCardNumber(cardNumber);
        user.setFullName(tcommaccount.getAccCname());
        user.setBalance(tcommaccount.getBalance());
        user.setOpeningDate(DateUtil.formatDate(tcommaccount.getAccCrdate(),"yyyy-MM-dd HH:mm:ss"));
        user.setLimitTimes1(255);
        user.setLimitTimes2(255);
        user.setLimitTimes3(255);
        user.setLimitTimes4(255);
        user.setConsumptionQuota(255.0000);
        user.setTermOfValidity(tcommaccount.getAccAvdate());

        return user;
    }

    @Override
    public Map findConsumeRecordList(String cardNumber, int pageIndex, int pageSize) {
        Ttnsmflwing ttnsmflwing = new Ttnsmflwing();
        ttnsmflwing.setFlwCrdid(cardNumber);
        Map map = this.merchantFeignClient.findByPage(pageIndex,pageSize,null,ttnsmflwing);
        List<LinkedHashMap<String,Object>> ttnsmflwingList = (List<LinkedHashMap<String,Object>>)map.get("data");
        if(ttnsmflwingList ==null || ttnsmflwingList.size()==0){
            throw new PosException(10028,"当前页不存在");
        }

        List<PosConsumeRecord> posConsumeRecordList = new ArrayList<PosConsumeRecord>();
        for(LinkedHashMap<String,Object> linkedHashMap : ttnsmflwingList){
            PosConsumeRecord posConsumeRecord = new PosConsumeRecord();
            posConsumeRecord.setStatus(0);
            for(Map.Entry<String, Object> entry : linkedHashMap.entrySet()) {
                if("flwContime".equalsIgnoreCase(entry.getKey())){
                    if(entry.getValue()!=null){
                        posConsumeRecord.setBizDate(entry.getValue().toString());
                    }
                }
                if("flwAmount".equalsIgnoreCase(entry.getKey())){
                    if(entry.getValue()!=null){
                        posConsumeRecord.setBizAmount((Double) entry.getValue());
                    }
                }
                if("accCname".equalsIgnoreCase(entry.getKey())){
                    if(entry.getValue()!=null){
                        posConsumeRecord.setFullName(entry.getValue().toString());
                    }
                }
                if("flwId".equalsIgnoreCase(entry.getKey())){
                    if(entry.getValue()!=null){
                        posConsumeRecord.setId( entry.getValue().toString());
                    }
                }
                if("flwCrdid".equalsIgnoreCase(entry.getKey())){
                    if(entry.getValue()!=null){
                        posConsumeRecord.setCardNumber( entry.getValue().toString());
                    }
                }
            }
            posConsumeRecordList.add(posConsumeRecord);
        }

        Map returnMap = new HashMap();
        returnMap.put("totalCount",map.get("count"));
        returnMap.put("data",posConsumeRecordList);
        return returnMap;
    }

    @Override
    public PosConsumptionStatistics getConsumptionStatistics(String mechineNumber) {
        Map map = this.merchantFeignClient.getCostSumAndCount(mechineNumber);
        PosConsumptionStatistics posConsumptionStatistics = new PosConsumptionStatistics();
        posConsumptionStatistics.setConsumptionMone(Double.parseDouble(map.get("sum").toString()));
        posConsumptionStatistics.setCounts(Integer.parseInt(map.get("count").toString()));
        posConsumptionStatistics.setMachineNumber(mechineNumber);
        return posConsumptionStatistics;
    }*/
}
